package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectJudge;
import com.jingdianjichi.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断类型的策略链子
 */
@Component
public class JudgeTypeHandler implements  SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入,是一对一的,所以我们不用list了
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);     //因为只有对错即可,其他什么都没有的

        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());

        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public void upDate(SubjectInfoBO subjectInfoBO) {
        //判断题目的插入,是一对一的,所以我们不用list了
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);     //因为只有对错即可,其他什么都没有的

        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());

        subjectJudgeService.update(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(long subjectId) {
        //判断题答案
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectJudge> result = subjectJudgeService.queryByCondition(subjectJudge);                 //实际里面就一个的
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConverter.INSTANCE.convertEntityToBoList(result);   //就是iscurrent这个字段

        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);         //answer是给简答题用的,其他的都是选项来的

        return subjectOptionBO;
    }
}
